﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class SSFGL_SSFGLAdd : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!ProjectPrivate.GetQX("10"))
      {
        Server.Transfer("~\\blank.aspx");
      }
      if (CWSD.GetSDZT("否"))
      {
        Server.Transfer("~\\sd.aspx");
      }
      string sID = CommonUse.GetID("jfsjid");
      if (!string.IsNullOrEmpty(sID))
      {
        ASPxHyperLink1.NavigateUrl = "SSFGLList.aspx?mode=0";
      }
      else
      {
        ASPxHyperLink1.NavigateUrl = "SSFGLKHList.aspx";
      }
      DataSet ds = FKFS.GetDS_W();
      CommonUse.BindDDL(ds, "fkid", "fkfs", "0", ddlFKFS);
      if (sID == "")
      {
        InitData();
      }
      else
      {
        if (JFSJ.ExistHQID(sID, CommonUse.GetID("khid")))
        {
          Response.Redirect("SSFGLList.aspx?mode=0");
        }
        InitDataUpdate(CommonUse.GetID("jfsjid"));
      }
    }
  }

  protected void InitDataUpdate(string sJFSJID)
  {
    lbKHBM.Text = CommonUse.GetID("khid");
    lbKHMC.Text = KHXX.GetKHMC(lbKHBM.Text);
    string sql = "select *,convert(varchar,JFSJ,23) SJ from sjlr_jfsj where jfsjid=@0";
    SqlParameter[] cmdParams = new SqlParameter[1] { new SqlParameter("@0", SqlDbType.VarChar, 20) };
    cmdParams[0].Value = sJFSJID;
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sql, cmdParams);
    if (dr.HasRows)
    {
      dr.Read();
      //lbBQYSK.Text = dr["bcysk"].ToString();
      lbBQJQK.Text = dr["bcjyk"].ToString();
      ddlFKFS.SelectedValue = dr["fkid"].ToString();
      hlFKFS.Value = ddlFKFS.SelectedValue;
      txtName.Text = dr["bqsj"].ToString();
      hlName.Value = txtName.Text;
      hlJFSJ.Value = dr["sj"].ToString();
    }
    dr.Close();

    double dSQJY = Convert.ToDouble(JFSJ.GetSCJY(lbKHBM.Text, sJFSJID));
    lbSQJCK.Text = dSQJY < 0 ? "0" : dSQJY.ToString();

    double dBQQF = Convert.ToDouble(CBSJ.GetFYCY(lbKHBM.Text, sJFSJID));
    lbBQQF.Text = dBQQF > 0 ? "0" : ((dBQQF * -1) + Convert.ToDouble(lbSQJCK.Text)).ToString();
    lbBQYSK.Text = dBQQF > 0 ? "0" : (dBQQF * -1).ToString();

    //lbSQJCK.Text = Math.Round(Convert.ToDouble(lbBQQF.Text) - Convert.ToDouble(lbBQYSK.Text), 2).ToString();
  }

  protected void InitData()
  {
    lbKHBM.Text = CommonUse.GetID("khid");
    lbKHMC.Text = KHXX.GetKHMC(lbKHBM.Text);
    //lbBQYSK.Text = (Convert.ToDouble(JFSJ.GetBCJQK(lbKHBM.Text)) * -1).ToString();
    //double dSQJY = Convert.ToDouble(KHXX.GetYE(lbKHBM.Text));
    double dSQJY = Convert.ToDouble(JFSJ.GetSCJY(lbKHBM.Text, CommonUse.GetID("jfsjid")));
    lbSQJCK.Text = dSQJY < 0 ? "0" : dSQJY.ToString();

    double dBQQF = Convert.ToDouble(CBSJ.GetFYCY(lbKHBM.Text));
    lbBQQF.Text = dBQQF > 0 ? "0" : ((dBQQF * -1) + Convert.ToDouble(lbSQJCK.Text)).ToString();

    //lbBQYSK.Text = ((Convert.ToDouble(lbSQJCK.Text) + dBQQF) > 0 ? 0 : (Convert.ToDouble(lbSQJCK.Text) + dBQQF) * -1).ToString();
    lbBQYSK.Text = dBQQF > 0 ? "0" : (dBQQF * -1).ToString();

    txtName.Text = lbBQYSK.Text;
  }

  protected void btnConfirm_Click(object sender, EventArgs e)
  {
    if (string.IsNullOrEmpty(CommonUse.GetID("jfsjid")))
      Insert();
    else
      Update();
  }

  //插入
  protected void Insert()
  {
    string sID = CommonUse.GetID("sjlr_jfsj", "jfsjid");
    Save(sID);
  }

  protected void Save(string sID)
  {
    if (!CommonUse.IsDouble(txtName))
    {
      lbMessage.Text = "金额必须为数字";
      return;
    }
    if (ddlFKFS.SelectedValue == "0")
    {
      lbMessage.Text = "请选择付款方式";
      return;
    }
    btnConfirm.Enabled = false;
    //string sKHID = CommonUse.GetID("khid");
    //DataTable dt = JFSJ.GetMX(lbKHBM.Text);
    //SqlParameter[] cmdParams = null;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    //CommandInfo ci;
    ArrayList arrValue = new ArrayList();
    ArrayList arrLengh = new ArrayList();
    //double dSQJY = Convert.ToDouble(lbSQJCK.Text) < 0 ? 0 : Convert.ToDouble(lbSQJCK.Text);
    //double dJE = Convert.ToDouble(txtName.Text) + Convert.ToDouble(lbSQJCK.Text);//账户余额
    //double dTemp = 0;

    #region  修改本期气量表，插入缴费明细
    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //      if (dJE > 0)
    //      {
    //        dJE = Math.Round(dJE + Convert.ToDouble(dt.Rows[i]["ssfy"].ToString()), 2);
    //        if (dJE >= 0)
    //        {
    //          sSql = "update sjlr_bqql set ssql=bqql,ssfy=ysfy,ssjj=ysjj,ssjg=ysjg where cbsjid=@0 and yqxzid=@1";
    //          cmdParams = new SqlParameter[2] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 10) };
    //          cmdParams[0].Value = dt.Rows[i]["cbsjid"].ToString();
    //          cmdParams[1].Value = dt.Rows[i]["yqxzid"].ToString();
    //          ci = new CommandInfo();
    //          ci.CommandText = sSql;
    //          ci.Parameters = cmdParams;
    //          cis.Add(ci);

    //          arrValue = new ArrayList();
    //          arrValue.Add(sID);
    //          arrValue.Add(dt.Rows[i]["yqxz"].ToString());
    //          arrValue.Add(dt.Rows[i]["jg"].ToString());
    //          arrValue.Add(Convert.ToDouble(dt.Rows[i]["ssql"].ToString()) * (-1));
    //          arrValue.Add(dt.Rows[i]["jj"].ToString());
    //          arrValue.Add(Convert.ToDouble(dt.Rows[i]["ssfy"].ToString()) * (-1));
    //          arrValue.Add(dt.Rows[i]["cbsjid"].ToString());
    //          arrLengh = new ArrayList();
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          cis.Add(CommonUse.InsertCI("sjlr_jfmx", arrValue, arrLengh));
    //        }
    //        else
    //        {
    //          double fy = Math.Round(dJE - Convert.ToDouble(dt.Rows[i]["ssfy"].ToString()), 2);
    //          dTemp = Convert.ToDouble(dt.Rows[i]["jg"].ToString()) + Convert.ToDouble(dt.Rows[i]["jj"].ToString());
    //          double ql = Math.Floor(fy / dTemp * 1000) / 1000;
    //          sSql = @"update sjlr_bqql set ssql=ssql+{1},ssfy={0},ssjj=floor({0}/(jg+jj))*jj,
    //ssjg=floor({0}/(jg+jj))*jg where cbsjid=@0 and yqxzid=@1";
    //          cmdParams = new SqlParameter[2] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 10) };
    //          cmdParams[0].Value = dt.Rows[i]["cbsjid"].ToString();
    //          cmdParams[1].Value = dt.Rows[i]["yqxzid"].ToString();
    //          ci = new CommandInfo();
    //          ci.CommandText = string.Format(sSql, dJE, Math.Round(ql, 3));
    //          ci.Parameters = cmdParams;
    //          cis.Add(ci);

    //          arrValue = new ArrayList();
    //          arrValue.Add(sID);
    //          arrValue.Add(dt.Rows[i]["yqxz"].ToString());
    //          arrValue.Add(dt.Rows[i]["jg"].ToString());
    //          arrValue.Add(Math.Round(ql, 3));
    //          arrValue.Add(dt.Rows[i]["jj"].ToString());
    //          arrValue.Add(fy);
    //          arrValue.Add(dt.Rows[i]["cbsjid"].ToString());
    //          arrLengh = new ArrayList();
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          cis.Add(CommonUse.InsertCI("sjlr_jfmx", arrValue, arrLengh));
    //          break;
    //        }
    //      }
    //      else
    //      {
    //        break;
    //      }
    //    }
    //    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    //    {
    //      lbMessage.Text = "保存成功";
    //    }

    #endregion

    #region  插入缴费数据

    double dBCJQK = Convert.ToDouble(txtName.Text) - Convert.ToDouble(lbBQYSK.Text);
    //lbBQJQK.Text = dJE <= 0 ? dBCJQK.ToString() : (Math.Round(dJE, 2)).ToString() + "&nbsp;";

    cis = new List<CommandInfo>();
    cis.Add(CommonUse.DeleteCI("sjlr_jfsj", "jfsjid", sID));

    arrValue = new ArrayList();
    arrValue.Add(sID);
    arrValue.Add(SysManage.Get_User);
    arrValue.Add(ddlFKFS.SelectedValue);
    arrValue.Add(lbKHBM.Text);
    arrValue.Add(DateTime.Now.ToString());
    arrValue.Add(txtName.Text);
    arrValue.Add("0");
    arrValue.Add(JFSJ.GetBH());
    arrValue.Add(dBCJQK < 0 ? dBCJQK * -1 : 0);
    arrValue.Add(dBCJQK < 0 ? 0 : dBCJQK);
    arrValue.Add(string.IsNullOrEmpty(lbBQYSK.Text) ? "0" : lbBQYSK.Text);
    arrValue.Add(lbSQJCK.Text);
    arrLengh = new ArrayList();
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    cis.Add(CommonUse.InsertCI("sjlr_jfsj", arrValue, arrLengh));

    sSql = "update jcxx_khxx set zhye=" + (dBCJQK < 0 ? 0 : dBCJQK) + " where khid=" + lbKHBM.Text;
    cis.Add(new CommandInfo(sSql, null));

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      ZHCX.CreateZHCX(lbKHBM.Text, DateTime.Now.ToString("yyyy-MM-dd"));
      ZHCX.UpdateJF(lbKHBM.Text, DateTime.Now.ToString("yyyy-MM-dd"));
      lbMessage.Text = "缴费成功";
      hlExport.NavigateUrl = "SSFGLExport.aspx?id=" + sID + "&khid=" + lbKHBM.Text;
      if (CommonUse.GetID("jfsjid") == "")
      {
        InitData();
      }
      else
      {
        InitDataUpdate(sID);
      }
      //lbSQJCK.Text = dJE <= 0 ? "0" : (Math.Round(dJE, 2)).ToString();
      //lbBQQF.Text = (dBCJQK * -1).ToString();
      //lbBQYSK.Text = (dJE <= 0 ? dBCJQK * -1 : 0).ToString();
    }
    #endregion
  }

  //修改
  protected void Update()
  {
    //Save(CommonUse.GetID("jfsjid"));
    if (!CommonUse.IsDouble(txtName))
    {
      lbMessage.Text = "金额必须为数字";
      return;
    }
    if (ddlFKFS.SelectedValue == "0")
    {
      lbMessage.Text = "请选择付款方式";
      return;
    }
    btnConfirm.Enabled = false;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    #region  插入缴费数据

    double dBCJQK = Convert.ToDouble(txtName.Text) - Convert.ToDouble(lbBQYSK.Text);
    //lbBQJQK.Text = dJE <= 0 ? dBCJQK.ToString() : (Math.Round(dJE, 2)).ToString() + "&nbsp;";

    sSql = "update sjlr_jfsj set fkid=@0,bqsj=@1,bcjqk=@2,bcjyk=@3,bcysk=@4 where jfsjid=@5";
    SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 20), new SqlParameter("@1", SqlDbType.VarChar, 20),
                               new SqlParameter("@2", SqlDbType.VarChar, 20), new SqlParameter("@3", SqlDbType.VarChar, 20),
                               new SqlParameter("@4", SqlDbType.VarChar, 20), new SqlParameter("@5", SqlDbType.VarChar, 20)};
    cmdParams[0].Value = ddlFKFS.SelectedValue;
    cmdParams[1].Value = txtName.Text;
    cmdParams[2].Value = dBCJQK < 0 ? dBCJQK * -1 : 0;
    cmdParams[3].Value = dBCJQK < 0 ? 0 : dBCJQK;
    cmdParams[4].Value = string.IsNullOrEmpty(lbBQYSK.Text) ? "0" : lbBQYSK.Text;
    cmdParams[5].Value = CommonUse.GetID("jfsjid");
    cis.Add(new CommandInfo(sSql, cmdParams));

    sSql = "update jcxx_khxx set zhye=" + (dBCJQK < 0 ? 0 : dBCJQK) + " where khid=" + lbKHBM.Text;
    cis.Add(new CommandInfo(sSql, null));
    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      string nr = "";
      if (txtName.Text != hlName.Value)
        nr += "本次收费由" + hlName.Value + "改为" + txtName.Text + "  ";
      if (ddlFKFS.SelectedValue != hlFKFS.Value)
        nr += "付款方式由" + ddlFKFS.Items.FindByValue(hlFKFS.Value).Text + "改为" + ddlFKFS.SelectedItem.Text + "  ";
      if (nr != "")
        rzcz.InsertRZ("修改抄表用户收费:客户编码为" + lbKHBM.Text + ",日期为" + hlJFSJ.Value + nr);
      ZHCX.CreateZHCX(lbKHBM.Text, hlJFSJ.Value);
      ZHCX.UpdateJF(lbKHBM.Text, hlJFSJ.Value);
      lbMessage.Text = "缴费成功";
      hlExport.NavigateUrl = "SSFGLExport.aspx?id=" + CommonUse.GetID("jfsjid") + "&khid=" + lbKHBM.Text;
      if (CommonUse.GetID("jfsjid") == "")
      {
        InitData();
      }
      else
      {
        InitDataUpdate(CommonUse.GetID("jfsjid"));
      }
      //lbSQJCK.Text = dJE <= 0 ? "0" : (Math.Round(dJE, 2)).ToString();
      //lbBQQF.Text = (dBCJQK * -1).ToString();
      //lbBQYSK.Text = (dJE <= 0 ? dBCJQK * -1 : 0).ToString();
    }
    #endregion
  }

  protected void btnDX_Click(object sender, EventArgs e)
  {
    ConvertSum cs = new ConvertSum();
    lbDX.Text = cs.ConvertS(txtName.Text);
  }
}
